package com.carnation.servlet;

import java.io.Writer;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.carnation.bean.GroupMenu;
import com.carnation.bean.HeadMenu;
import com.carnation.bean.Menu;
import com.carnation.utils.Json;
import com.carnation.utils.MenuRead;
import com.carnation.utils.MenuWrite;
import com.google.gson.Gson;

public class ModMenuServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(ModMenuServlet.class);

	@Override
	protected void doPost(HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) {
		doGet(httpServletRequest, httpServletResponse);
	}

	@Override
	protected void doGet(HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) {
		String type = httpServletRequest.getParameter("type");
		logger.info(type);
		Json json = new Json();
		String msg = null;
		String status = null;
		if(httpServletRequest.getSession().getAttribute("sessionInfo") == null){
			msg = "登陆超时,请重新登陆";
			status="500";
			json.setMsg(msg);
			json.setStatus(status);
			Gson g = new Gson();
			try{
				httpServletResponse.setCharacterEncoding("UTF-8");
				httpServletResponse.getWriter().write(g.toJson(json));
				httpServletResponse.getWriter().close();
			}
			catch(Exception e){
				logger.error("响应书写失败",e);
			}
			return;
		}
		MenuRead menuRead = new MenuRead();
		MenuWrite menuWrite = new MenuWrite();
		if (type.equals("head")) {
			String id = httpServletRequest.getParameter("id");
			String name = httpServletRequest.getParameter("name");
			String title = httpServletRequest.getParameter("title");
			String icon = httpServletRequest.getParameter("icon");
//			String parentId = menuRead.getParentIdByIdAndType(id, type);
			if (!menuRead.checkHeadNameExists(name,id)) {
				HeadMenu headMenu = new HeadMenu(id, name, title, name, icon);
				if (menuWrite.modifyHead(headMenu)) {
					msg = "修改头部菜单成功";
					status = "200";
				} else {
					msg = "修改头部菜单失败";
					status = "400";
				}
			} else {
				msg = "修改名称与兄弟节点冲突";
				status = "300";
			}
		} else if (type.equals("group")) {
			String id = httpServletRequest.getParameter("id");
			String name = httpServletRequest.getParameter("name");
			String icon = httpServletRequest.getParameter("icon");
			String parentId = menuRead.getParentIdByIdAndType(id, type);
			if (parentId == null) {
				msg = "找不到父节点";
				status = "400";
			} else {
				if (menuRead.checkGroupNameExists(parentId, name,id)) {
					msg = "修改名称与兄弟节点冲突";
					status = "300";
				} else {
					GroupMenu groupMenu = new GroupMenu(id, name, icon);
					if (menuWrite.modifyGroup(groupMenu)) {
						msg = "修改组菜单成功";
						status = "200";
					} else {
						msg = "修改组菜单失败";
						status = "400";
					}
				}
			}
		} else if (type.equals("menu")) {
			String id = httpServletRequest.getParameter("id");
			String name = httpServletRequest.getParameter("name");
			String icon = httpServletRequest.getParameter("icon");
			String parentId = menuRead.getParentIdByIdAndType(id, type);
			if (parentId == null) {
				msg = "找不到父节点ID";
				status = "400";
			} else {
				if (menuRead.checkMenueNameExists(parentId, name,id)) {
					msg = "修改名称与兄弟节点冲突";
					status = "300";
				} else {
					Menu menu = new Menu(id, name, icon, null);
					if (menuWrite.modfiyMenu(menu)) {
						msg = "修改菜单成功";
						status = "200";
					} else {
						msg = "修改菜单失败";
						status = "400";
					}
				}
			}

		} else {
			msg = "未知类型";
			status = "500";
		}
		json.setMsg(msg);
		json.setStatus(status);
		Gson gson = new Gson();
		httpServletResponse.setCharacterEncoding("UTF-8");
		try {
			Writer writer = httpServletResponse.getWriter();
			writer.write(gson.toJson(json));
		} catch (Exception e) {
			logger.error("发送响应失败", e);
		}

	}
}
